package Component;

import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONArray;
import Clock.Clock;
import GameObj.GameObject;

import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector2; 


import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.BodyDef;
import com.badlogic.gdx.physics.box2d.CircleShape; 
import com.badlogic.gdx.physics.box2d.PolygonShape; 
import com.badlogic.gdx.physics.box2d.Shape; 
import com.badlogic.gdx.physics.box2d.Body; 
import Scene.PhyWorld;
import Scene.SceneMgr;
import com.badlogic.gdx.physics.box2d.Fixture; 
public class PhysicComponent extends GameComponent 
{
	Vector2 vGravity = null;
	public BodyDef bodyDef = null;
	CircleShape circleShape = null;
	PolygonShape polygonShape = null;
	public float density = 0.0f;
	public float friction = 0.0f;
	public Body body_ = null;
	public Fixture fixture = null;

	
	public boolean bAddToWorld = false;
	
	public Rectangle boundingbox = new Rectangle();
	
	public void SetFixture(Fixture ft)
	{
		fixture = ft;
	}
	
	
	public int GetTypeId()
	{
		return E_PHYSICOMPONENT;
	}
	
	public Shape GetShape()
	{
		return (polygonShape == null) ? circleShape : polygonShape;			
	}
	
	public boolean Load(JSONObject jsonComp)
	{
		super.Load(jsonComp);
		boolean bResult = false;
		try
		{	 
			if(jsonComp.has("Gx"))
			{
				vGravity.x = jsonComp.getInt("Gx");
			}
			
			if(jsonComp.has("Gy"))
			{
				vGravity.y = jsonComp.getInt("Gy");
			}
			
			if(jsonComp.has("BodyDef"))
			{
				//String strBodyDef = jsonComp.getString("BodyDef");
				bodyDef = new BodyDef();
				JSONObject bodydefJson = jsonComp.getJSONObject("BodyDef");
				if (bodydefJson.has("pos"))
				{
					JSONArray arrPos = bodydefJson.getJSONArray("pos");
					bodyDef.position.x = (float)arrPos.getDouble(0);
					bodyDef.position.y = (float)arrPos.getDouble(1);						
				}
				else
				{
					// 
					bodyDef.position.x = mHost.GetPos().x / SceneMgr.lvl.physcale;
					bodyDef.position.y = mHost.GetPos().y / SceneMgr.lvl.physcale;				
				}
				
				if (bodydefJson.has("btype"))
				{
					int bType = bodydefJson.getInt("btype");
					bodyDef.type = (BodyDef.BodyType.values()[bType]);			
				}
				
				if (bodydefJson.has("angDamping"))
				{
					float angdamping = (float)bodydefJson.getDouble("angDamping");
					bodyDef.angularDamping = angdamping;	
				}
				
				
			}
			
			
			if (jsonComp.has("circleshp"))
			{
				JSONObject circleshp = jsonComp.getJSONObject("circleshp");
				circleShape = new CircleShape();
				int radius = circleshp.getInt("r");
				circleShape.setRadius(radius);
				//circleShape.setPosition(position);
			}
			
			
			if (jsonComp.has("polyshp"))
			{
				JSONObject polyshp = jsonComp.getJSONObject("polyshp");
				polygonShape = new PolygonShape();

				if (polyshp.has("vertice"))
				{
					JSONArray vers = polyshp.getJSONArray("vertice");
					Vector2[] vertices = new Vector2[(vers.length() + 1)/ 2];
					int j = 0;
					for (int i = 0; i < vertices.length; ++i)
					{						
						vertices[i] = new Vector2((float)vers.getDouble(j++), (float)vers.getDouble(j++));
						if (j >= vers.length())
						{
							break;						
						}					
					}
					polygonShape.set(vertices);				
				}
				
				if (polyshp.has("box"))
				{
					JSONObject box = polyshp.getJSONObject("box");
					polygonShape.setAsBox((float)box.getDouble("x"), (float)box.getDouble("y"));					
				}
				

			}
			
			// 
			if (jsonComp.has("Density"))
			{
				density = (float)jsonComp.getDouble("Density");
			}
			
			if (jsonComp.has("friction"))
			{
				friction = (float)jsonComp.getDouble("friction");
			}
			
			if (enable)
			{
				PhyWorld.addInitCompoent(this);
			}		
			

		}
		catch(JSONException e)
		{
			
		}
		
		return bResult;		
	}
	
	public void SetEnable(boolean bEnable)
	{
		super.SetEnable(bEnable);
		if (bEnable)
		{
			PhyWorld.addCompoent(this);
		}
		else
		{
			PhyWorld.DelCompoent(this);
		
		}
		
	}

	public boolean GetEnable()
	{
		return super.GetEnable();		
	}
	
	public void Update(Clock clock)
	{
		if (!enable)
		{
			return;
		}
		
		Vector2 v = body_.getPosition();
		v.mul(SceneMgr.lvl.physcale);
		mHost.SetPos(v);
		
		float fRot = body_.getAngle();
		mHost.SetRot(fRot);
	}
}
